package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.TalentUser;
import org.dromara.system.domain.bo.TalentUserBo;
import org.dromara.system.domain.bo.WechatQueryRequestBo;
import org.dromara.system.domain.vo.TalentUserVo;
import org.dromara.system.domain.vo.WechatTalentUserVo;
import org.dromara.system.mapper.TalentUserMapper;
import org.dromara.system.service.ITalentUserService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 人才信息Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TalentUserServiceImpl implements ITalentUserService {

    private final TalentUserMapper baseMapper;

    // 定义手机号的正则表达式
    private static final String PHONE_REGEX = "^1\\d{10}$";

    // 编译正则表达式
    private static final Pattern pattern = Pattern.compile(PHONE_REGEX);


    /**
     * 查询人才信息
     *
     * @param userId 主键
     * @return 人才信息
     */
    @Override
    public TalentUserVo queryById(Long userId) {
        return baseMapper.selectVoById(userId);
    }

    /**
     * 分页查询人才信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 人才信息分页列表
     */
    @Override
    public TableDataInfo<TalentUserVo> queryPageList(TalentUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TalentUser> lqw = buildQueryWrapper(bo);
        Page<TalentUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的人才信息列表
     *
     * @param bo 查询条件
     * @return 人才信息列表
     */
    @Override
    public List<TalentUserVo> queryList(TalentUserBo bo) {
        LambdaQueryWrapper<TalentUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TalentUser> buildQueryWrapper(TalentUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TalentUser> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TalentUser::getUserId);
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), TalentUser::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), TalentUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getPhonenumber()), TalentUser::getPhonenumber, bo.getPhonenumber());
        lqw.like(StringUtils.isNotBlank(bo.getWorkCompanyName()), TalentUser::getWorkCompanyName, bo.getWorkCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCompanyType()), TalentUser::getWorkCompanyType, bo.getWorkCompanyType());
        lqw.eq(StringUtils.isNotBlank(bo.getXiangzhen()), TalentUser::getXiangzhen, bo.getXiangzhen());
        lqw.eq(StringUtils.isNotBlank(bo.getLearningForms()), TalentUser::getLearningForms, bo.getLearningForms());
        lqw.eq(StringUtils.isNotBlank(bo.getDegree()), TalentUser::getDegree, bo.getDegree());
        lqw.eq(StringUtils.isNotBlank(bo.getNewFengjieTalentLevel()), TalentUser::getNewFengjieTalentLevel, bo.getNewFengjieTalentLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getProfessionalSkillLevel()), TalentUser::getProfessionalSkillLevel, bo.getProfessionalSkillLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getSkillTalentType()), TalentUser::getSkillTalentType, bo.getSkillTalentType());
        lqw.eq(StringUtils.isNotBlank(bo.getHangyeType()), TalentUser::getHangyeType, bo.getHangyeType());
        lqw.eq(StringUtils.isNotBlank(bo.getIndustryCategory()), TalentUser::getIndustryCategory, bo.getIndustryCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getJobCategory()), TalentUser::getJobCategory, bo.getJobCategory());
        lqw.eq(bo.getExpireDate() != null, TalentUser::getExpireDate, bo.getExpireDate());
        return lqw;
    }

    /**
     * 新增人才信息
     *
     * @param bo 人才信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TalentUserBo bo) {
        TalentUser add = MapstructUtils.convert(bo, TalentUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改人才信息
     *
     * @param bo 人才信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TalentUserBo bo) {
        TalentUser update = MapstructUtils.convert(bo, TalentUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public List<TalentUserVo> queryByPhoneNumber(String phoneNumber) {
        LambdaQueryWrapper<TalentUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(phoneNumber), TalentUser::getPhonenumber, phoneNumber);
        return baseMapper.selectVoList(queryWrapper);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TalentUser entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除人才信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public WechatTalentUserVo queryTalentForWechat(WechatQueryRequestBo bo) {
        if (StringUtils.isEmpty(bo.getPhonenumber())) {
            throw new ServiceException("手机号码不能为空");
        }
        Matcher matcher = pattern.matcher(bo.getPhonenumber());
        boolean matches = matcher.matches();
        if (!matches) {
            throw new ServiceException("请输入正确的手机号码格式");

        }
        WechatTalentUserVo wechatTalentUserVo = baseMapper.queryTalentForWechat(bo);
        if (wechatTalentUserVo == null) {
            throw new ServiceException("未查询到对应的人员信息\n(如果您符合新奉节人才标准还没申报，请您向您工作的县级行业主管部门或县人力社保局申报)");
        }
        return wechatTalentUserVo;
    }


}
